//////////////////////////////////////
// Fast Container Library           //
// MIT License - Copyright (c) 2012 //
//////////////////////////////////////

#pragma once

#include "common.h"

FC_NAMESPACE_BEGIN


inline int sdbm_hash( int hash, char c )
{
	return c + (hash << 6) + (hash << 16) - hash;
}

inline size_t fnv_hash( const void* key, size_t n )
{
	const unsigned char* p = (const unsigned char*)key;
	size_t h = 2166136261;
	for( size_t i(0); i != n; ++i )
		h = (h * 16777619) ^ p[i];

	return h;
}

inline size_t fnv_hash( const char* key )
{
	const unsigned char* p = (const unsigned char*)key;
	size_t h = 2166136261;
	while( *p != 0 )
		h = (h * 16777619) ^ *p++;

	return h;
}


// "MurmurHash2" code taken from https://sites.google.com/site/murmurhash/. (public domain)
// It offers very good distribution and excellent performance.

inline size_t murmur_hash( const void * key, int len, size_t seed )
{
	const size_t m = 0x5bd1e995;
	const int r = 24;

	size_t h = seed ^ len;
	const unsigned char * data = (const unsigned char *)key;

	while( len >= 4 )
	{
		size_t k = *(size_t*)data;

		k *= m;
		k ^= k >> r;
		k *= m;

		h *= m;
		h ^= k;

		data += 4;
		len -= 4;
	}

	switch( len )
	{
		case 3: h ^= data[2] << 16;
		case 2: h ^= data[1] << 8;
		case 1: h ^= data[0];
		h *= m;
	};

	h ^= h >> 13;
	h *= m;
	h ^= h >> 15;

	return h;
}




FC_NAMESPACE_END
